<template lang="pug">
data-page(
  v-bind="pageParams"
)
  mt-table(
    v-bind="tableProps"
  )
projectSelect(v-model="projectSelectModel",:selectEquipment="selectEquipmentItem",:selectAction="projectAction",@handeSelect="handeSelect")
</template>

<script setup lang="ts">
import {
  type FormItemProps,
  type IAction,
  type DataPageProps,
  type Action,
  type TableProps,
  useNotify,
} from "@scmt/common-components";
import { useRequest } from "@scmt/common-hooks";
import { type QueryPagination, useApi } from "@scmt/core-api";
import type { ProjectEntity, ProjectQueryParams } from "@scmt/api-pacs-project";
import projectSelect from "./projectSelect.vue";
//添加 Props属性
const props = defineProps({
  selectEquipment: Object,
});
const projectSelectModel = ref(false);
const projectAction = ref("");
const selectEquipmentItem = ref();
const $api = useApi();
const $notify = useNotify();
const queryPramas = reactive<ProjectQueryParams>({
  name: "",
  pageNum: 1,
  pageSize: 20,
});
const editingObj = ref<null | ProjectEntity>(null);

const { request, data, loadingMap } = useRequest({
  tableData: (q: any) => {
    let params = Object.assign(
      { deviceId: props.selectEquipment?.value?.id },
      queryPramas,
      q
    );
    if (!props.selectEquipment || !props.selectEquipment.id) {
      $notify.warning("请先选择设备");
      //没有选择设备的话 deviceId传一个不会有的值
      params = Object.assign(
        { deviceId: "testtt", projectType: "dr" },
        queryPramas,
        q
      );
    } else {
      params = Object.assign(
        {
          deviceId: props.selectEquipment?.id,
          projectType: props.selectEquipment?.inspectionTypeId,
        },
        queryPramas,
        q
      );
    }
    return $api.pacs.project.queryPacsProjectPageListByDeviceId(params);
  },

  save: (model: ProjectEntity) => {
    return $api.pacs.project.save(model).then(() => {
      $notify.success("保存成功");
      editingObj.value = null;

      request("tableData", {
        pageSize: 20,
      });
    });
  },
  destroy: (ids: string) => {
    return $api.pacs.project.destroy(ids).then(() => {
      $notify.success("删除成功");

      request("tableData", {
        pageSize: 20,
      });
    });
  },
});

const pageParams = computed<DataPageProps>(() => {
  const queryFields: FormItemProps[] = [
    {
      prop: "projectName",
      label: "项目名称",
    },
    // {
    //   prop: "projectCode",
    //   label: "项目编码",
    // },
    // {
    //   prop: "projectType",
    //   label: "项目类型",
    //   as: "select",
    //   options: {
    //     loadMethod: $api.pacs.inspectionType.listAsOptions,
    //   },
    // },
  ];

  const headerActions: Action[] = [
    { type: "primary", label: "批量关联", event: "association" },
    { type: "warning", label: "批量取消", event: "cancellation" },
  ];

  const dataPageProps: DataPageProps = {
    loading: loadingMap.tableData,
    queryModel: queryPramas,
    queryFields,
    actions: headerActions,
    onQuery: () => request("tableData"),
    onAct: onPageActions,
    columns: 3,
  };

  return dataPageProps;
});

const tableProps = computed(() => {
  const { tableData } = toRefs(data);

  const tableProps: TableProps = {
    loading: loadingMap.tableData,
    data: tableData.value?.items,
    pagination: tableData.value?.pagination,
    columns: [
      {
        type: "index",
        label: "序号",
        width: "60px",
      },
      {
        prop: "projectName",
        label: "项目名称",
        tooltip: true,
        minWidth: "120px",
      },
      {
        prop: "projectCode",
        label: "项目编码",
        tooltip: false,
        minWidth: "120px",
      },
      {
        prop: "entryTypeName",
        label: "项目类型",
        tooltip: false,
        minWidth: "120px",
      },
      {
        prop: "officeName",
        label: "所属科室",
        minWidth: "120px",
      },
      {
        prop: "basicPrice",
        label: "基础价格",
        tooltip: false,
        minWidth: "120px",
      },
      {
        prop: "createTime",
        label: "创建时间",
        tooltip: true,
        minWidth: "120px",
        format: "datetime",
      },
    ],
    onAct: onPageActions,
    onPaginate: (pagination: QueryPagination) =>
      request("tableData", pagination),
  };
  return tableProps;
});

const onPageActions = (action: IAction, model?: any) => {
  switch (action.event) {
    case "add":
    case "edit":
      editingObj.value = $api.pacs.project.$$createEntity(model);
      break;
    case "destroy":
      request("destroy", model ? model.id : "");
      break;
    case "association":
      if (!props.selectEquipment?.id) {
        $notify.warning("请先选择设备");
        break;
      }
      projectSelectModel.value = true;
      projectAction.value = "关联";
      break;
    case "cancellation":
      if (!props.selectEquipment?.id) {
        $notify.warning("请先选择设备");
        break;
      }
      projectSelectModel.value = true;
      projectAction.value = "取消";
      break;
  }
};
const handeSelect = () => {
  request("tableData");
};
watch(
  () => props.selectEquipment,
  (item) => {
    if (item && item.id) {
      request("tableData");
      selectEquipmentItem.value = item;
    }
  },
  {
    immediate: true,
  }
);
</script>

<style lang="less" scoped></style>
